#! /usr/bin/env python

import mathex

def isWanted(n):
    s = str(n)
    l = len(s)
    s = s*2
    for i in range(l):
        if not mathex.isPrime(int(s[i:i+l])):
            return False
    return True
c=0
for n in range(1,1000000):
    if isWanted(n):
        c += 1

print c
